package com.testdocs.client.test.group.rename;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.testdocs.client.common.dialog.base.DialogPanel;
import com.testdocs.client.test.i18n.TestConstants;
import com.testdocs.client.validation.FieldConstraint;
import com.testdocs.client.validation.ValidationConstraint;

public class RenameDialogPanel extends Composite implements DialogPanel {
	private static RenameDialogPanelUiBinder uiBinder = GWT.create(RenameDialogPanelUiBinder.class);

	@UiTemplate("RenameDialogPanel.ui.xml")
	interface RenameDialogPanelUiBinder extends UiBinder<Widget, RenameDialogPanel> {
	}

	@UiField
	Label nameLabel;
	@UiField
	TextBox nameTextBox;
	@UiField
	Label nameErrorLabel;

	private TestConstants testConstants = (TestConstants) GWT.create(TestConstants.class);

	public RenameDialogPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		nameLabel.setText(testConstants.groupName());
	}
	
	public void setName(String name) {
		nameTextBox.setText(name);
	}
	
	public String getName() {
		return nameTextBox.getText();
	}
	
	public void handleValidationConstraints(ValidationConstraint[] constraints) {
		for (ValidationConstraint constraint : constraints) {
			if (constraint instanceof FieldConstraint
					&& ((FieldConstraint) constraint).getField().equals(
							"name")) {
				nameErrorLabel.setText(constraint.getMessage());
			}
		}

	}

	@Override
	public void clear() {
		nameTextBox.setText("");
		nameErrorLabel.setText("");
	}
}
